home *** CD-ROM | disk | FTP | other *** search
- Path: navet.enator.se!usenet
- From: Enator A/S <ENATOR@ENATOR.DK>
- Newsgroups: comp.lang.c++
- Subject: Re: OO design issue
- Date: 9 Jan 1996 17:28:14 GMT
- Organization: Enator A/S
- Message-ID: <4cu8je$kgf@navet.enator.se>
- References: <4ctp93$2gu@news.kth.se>
- NNTP-Posting-Host: 193.162.31.48
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 1.1N (Windows; I; 16bit)
-
- Dear Anders,
-
- If I got you right, your problem is very common, and the design issue is
- a very important one.
-
- Of course you can just let your member-classes have a reference to the
- parent-class, but it is very bad design. You should allways try to make a
- simple object-composition hiararchy, and that means: memberobjects do
- not know parents. One of the reasons is reuseability - if the member has
- a reference to the parent, the member can't be used in an environment,
- where the parent does not exists.
-
- The solution is described in the 'Design Pattern' book by Erich Gamma and
- others, and it is called an (plug-in)adapter. You get nothing for free,
- and what you have to pay here is the cost of two new classes.
-
- Together with your Triangle (member) class you supply an abstract base
- class TriangleAdapter and your Triangle constructor must have a pointer
- to a TriangleAdapter as a parameter, and save it for later use. The
- TriangleAdapter must have a pure virtual function: GetXCoordinate, which
- can be called from the Triangle class. A class, which wants to use a
- Triangle object now has to derive a class from TriangleAdapter and
- implement the function GetXCoordinate here. That means your parent
- TrianglesAndNodes must make an instance of
- a TriangelAdapterTrianglesAndNodes class which has implemented
- GetXCoordinate, by calling GetXCoordinate in TrianglesAndNodes, and
- handle a reference to this adapter over to the Triangle object.
-
- The magic is, that by using the virtual mechanism you can invert the
- direction of the relation and thereby loosen up constraints. This ability
- makes the adapter pattern very important to C++ programmers.
-
-
- Hardy Henneberg
- ENATOR Danmark
-
-
-